<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="交接内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入交接内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basicInformation:shifts:add']"
        >新增
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['basicInformation:shifts:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basicInformation:shifts:remove']"
        >批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basicInformation:shifts:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="setPrint"
          v-hasPermi="['frontierEvent:shifts:export']"
        >打印
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shiftsList" @selection-change="handleSelectionChange" id="tableList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="被交接人" align="center" prop="byTransferName"/>
      <el-table-column label="交接人" align="center" prop="transferName"/>
      <el-table-column label="交接内容" align="center" prop="content">
      <template slot-scope="scope">
        <span v-html="scope.row.content">{{scope.row.content}}</span>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basicInformation:shifts:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basicInformation:shifts:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改交接班记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="被交接人" prop="befCrewId">
          <el-input v-model="form.byTransferName" placeholder="请选择被交接人" disabled="true" style="width: 200px"/>
          <el-button type="primary" v-model="form.befCrewId" @click="selectPatrol(1)"style="margin-left: 10px">选择被交接人</el-button>
        </el-form-item>
        <el-form-item label="交接人" prop="aftCrewId">
          <el-input v-model="form.transferName" placeholder="请选择交接人"  disabled="true"  style="width: 200px"/>
          <el-button type="primary" v-model="form.aftCrewId" @click="selectPatrol(2)" style="margin-left: 10px">选择交接人</el-button>
        </el-form-item>
        <el-form-item label="交接内容">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="patrolTitle" :visible.sync="patrolOpen" width="800px" append-to-body>
      <el-form :model="queryPatrolParams" ref="queryPatrolForm" :inline="true" v-show="showSearchPatrol" label-width="68px">
        <el-form-item label="登录名" prop="userName">
          <el-input
            v-model="queryPatrolParams.userName"
            placeholder="请输入登录名"
            clearable
            size="small"
            @keyup.enter.native="handleQueryPatrol"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryPatrol">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetPatrolQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="patrolList" @selection-change="handleSelectionChangeTerminal">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户id" align="center" prop="userId" />
        <el-table-column label="登录名" align="center" prop="userName" />
        <el-table-column label="手机号" align="center" prop="phonenumber" />
      </el-table>
      <pagination
        v-show="totalPatrol>0"
        :total="totalPatrol"
        :page.sync="queryPatrolParams.pageNum"
        :limit.sync="queryPatrolParams.pageSize"
        @pagination="getPatrolList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okPatrol">确 定</el-button>
        <el-button @click="cancelPatrol">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listAppUser} from "@/api/basicInformation/appUser";
  import {
    listShifts,
    getShifts,
    delShifts,
    addShifts,
    updateShifts,
    exportShifts
  } from '@/api/basicInformation/shifts'
  import Editor from '@/components/Editor'

  export default {
    name: 'Shifts',
    components: {
      Editor
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 交接班记录表格数据
        shiftsList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          content: undefined,
          orderByColumn: 'id',
          isAsc: 'asc'
        },
        //搜索巡护人员用到字段
        idsPatrol: [],
        patrolNames: [],
        patrolNumbers: [],
        totalPatrol: 0,
        patrolList: [],
        // 显示搜索条件
        showSearchPatrol: true,
        // 选择人员弹出层标题
        patrolTitle: '选择巡护人员',
        // 是否显示选择人员弹出层
        patrolOpen: false,
        // 查询巡护人员                                                                                                                                                                                                                                        参数
        queryPatrolParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          userType: 2
        },
        patrolType: 1,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          befCrewId: [
            { required: true, message: '被交接人不能为空', trigger: 'blur' }
          ],
          aftCrewId: [
            { required: true, message: '交接人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      setPrint() {
        // var html = document.getElementById("tableList").innerHTML;
        var w = window.open(location.href)
        // var style = w.document.createElement("style");
        // style.type = "text/css";
        // style.innerText = `打印内容元素的样式...`;
        var tableStr = '<table style=\'width:100%;\'><tr><td style=\'text-align: center;\'>被交接人</td><td style=\'text-align: center;\'>交接人</td><td style=\'text-align: center;\'>交接内容</td></tr>'
        for (var i = 0; i < this.shiftsList.length; i++) {
          var byTransferName = this.shiftsList[i].byTransferName === null ? '' : this.shiftsList[i].byTransferName
          var transferName = this.shiftsList[i].transferName === null ? '' : this.shiftsList[i].transferName
          var content = this.shiftsList[i].content === null ? '' : this.shiftsList[i].content
          tableStr += '<tr><td style=\'text-align: center;\'>' + byTransferName + '</td><td style=\'text-align: center;\'>' + transferName + '</td><td style=\'text-align: center;\'>' + content + '</td></tr>'
        }
        tableStr += '</table>'
        w.document.body.innerHTML = tableStr
        w.document.close()
        w.print()
        w.close()
      },
      /** 查询交接班记录列表 */
      getList() {
        this.loading = true
        listShifts(this.queryParams).then(response => {
          this.shiftsList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      /** 获取巡护人员列表 */
      getPatrolList() {
        this.loading = true;
        listAppUser(this.queryPatrolParams).then(response => {
          this.patrolList = response.rows;
          this.totalPatrol = response.total;
          this.loading = false;
        });
      },
      /** 巡护人员搜索按钮操作 */
      handleQueryPatrol() {
        this.queryPatrolParams.pageNum = 1;
        this.getPatrolList();
      },
      /** 巡护人员重置按钮操作 */
      resetPatrolQuery() {
        this.resetForm("queryPatrolForm");
        this.handleQueryPatrol();

        // this.handleQueryTerminal();
      },
      /** 选择巡护人员*/
      selectPatrol(type){
        this.idsPatrol=[];
        this.patrolNames=[];
        this.patrolType = type;
        this.patrolOpen = true;
        this.resetForm("queryPatrolForm");
        this.handleQueryPatrol();
      },
      // 多选框选中数据
      handleSelectionChangeTerminal(selection) {
        this.idsPatrol= selection.map(item => item.userId)
        this.patrolNames = selection.map(item => item.userName);
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          befCrewId: undefined,
          byTransferName: undefined,
          aftCrewId: undefined,
          transferName: undefined,
          content: undefined,
          delFlag: undefined,
          createById: undefined,
          createBy: undefined,
          createTime: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.names = selection.map(item => item.transferName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加交接班记录'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getShifts(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改交接班记录'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateShifts(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addShifts(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        const names = row.transferName || this.names
        this.$confirm('是否确认删除交接人为"' + names + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delShifts(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有交接班记录数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportShifts(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      },
      okPatrol() {
        if (this.idsPatrol.length > 1) {
          this.msgInfo("只能选择一个巡护人员");
        } else if (this.idsPatrol.length === 1) {
          if(this.patrolType === 1){
            this.form.befCrewId = this.idsPatrol[0];
            this.form.byTransferName = this.patrolNames[0];
          }else{
            this.form.aftCrewId = this.idsPatrol[0];
            this.form.transferName = this.patrolNames[0];
          }
          this.patrolOpen = false;
        } else {
          this.msgInfo("请选择巡护人员");
        }
      },
      cancelPatrol() {
        this.patrolOpen = false;
      }
    }
  }
</script>
